﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;

namespace SF.Utilities
{
    public class DrawHelper
    {
        public class WebPageBitmap
        {
            WebBrowser MyBrowser;

            /// <summary>
            /// 网页地址
            /// </summary>
            string Url {get;set;}

            /// <summary>
            /// 高度
            /// </summary>
            int Height { get; set; }

            /// <summary>
            /// 宽度
            /// </summary>
            int Width { get; set; }

            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="url">网页地址</param>
            /// <param name="width">宽度</param>
            /// <param name="height">高度</param>
            public WebPageBitmap(string url, int width, int height)
            {
                this.Height = height;
                this.Width = width;
                this.Url = url;
                MyBrowser = new WebBrowser();
                MyBrowser.ScrollBarsEnabled = false;
                MyBrowser.Size = new Size(this.Width, this.Height);
            }

            public void GetIt()
            {
                MyBrowser.Navigate(this.Url);
                while (MyBrowser.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }
            }

            /// <summary>
            /// 截屏
            /// </summary>
            /// <param name="theight">高度</param>
            /// <param name="twidth">宽度</param>
            /// <returns>返回截屏的位图对象</returns>
            public Bitmap DrawBitmap(int theight, int twidth)
            {
                Bitmap myBitmap = new Bitmap(this.Width, this.Height);
                Rectangle DrawRect = new Rectangle(0, 0, this.Width, this.Height);
                MyBrowser.DrawToBitmap(myBitmap, DrawRect);
                System.Drawing.Image imgOutput = myBitmap;
                System.Drawing.Image oThumbNail = new Bitmap(twidth, theight, imgOutput.PixelFormat);
                Graphics g = Graphics.FromImage(oThumbNail);
                g.CompositingQuality = CompositingQuality.HighSpeed;
                g.SmoothingMode = SmoothingMode.HighSpeed;
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                Rectangle oRectangle = new Rectangle(0, 0, twidth, theight);
                g.DrawImage(imgOutput, oRectangle);
                try
                {
                    return (Bitmap)oThumbNail;
                }
                catch (Exception ex)
                {
                    Log4Helper.Info("画图过程中出现异常：" + ex.Message);
                    return null;
                }
                finally
                {
                    imgOutput.Dispose();
                    imgOutput = null;
                    MyBrowser.Dispose();
                    MyBrowser = null;
                }
            }
        }

        public class GetImage
        {
            /// <summary>
            /// 屏幕高度
            /// </summary>
            public int ScreenHeight { get; set; }

            /// <summary>
            /// 屏幕宽度
            /// </summary>
            public int ScreenWidth { get; set; }

            /// <summary>
            /// 图片高度
            /// </summary>
            public int ImageHeight { get; set; }

            /// <summary>
            /// 图片宽度
            /// </summary>
            public int ImageWidth { get; set; }

            /// <summary>
            /// 网页地址
            /// </summary>
            public string Url { get; set; }

            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="url">网页地址</param>
            /// <param name="browserBaseInfo">浏览器对象</param>
            public GetImage(string url, System.Web.HttpBrowserCapabilitiesBase browserBaseInfo)
            {
                this.Url = url;
                this.ScreenWidth = browserBaseInfo.ScreenPixelsWidth;
                this.ScreenHeight = browserBaseInfo.ScreenPixelsHeight;
                this.ImageHeight = this.ScreenHeight;
                this.ImageWidth = this.ScreenWidth;
                Log4Helper.Info("屏幕宽度高度分别为：" + this.ScreenWidth + "," + this.ScreenHeight);
                Log4Helper.Info("屏幕宽度1高度1分别为：" + browserBaseInfo.ScreenCharactersWidth + "," + browserBaseInfo.ScreenCharactersHeight);
            }

            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="url">网页地址</param>
            /// <param name="screenWidth">屏幕宽度</param>
            /// <param name="screenHeight">屏幕高度</param>
            /// <param name="imageWidth">图片宽度</param>
            /// <param name="imageHeight">图片高度</param>
            public GetImage(string url, int screenWidth, int screenHeight, int imageWidth, int imageHeight)
            {
                this.Url = url;
                this.ScreenWidth = screenWidth;
                this.ScreenHeight = screenHeight;
                this.ImageHeight = imageHeight;
                this.ImageWidth = imageWidth;
            }

            /// <summary>
            /// 获取位图
            /// </summary>
            /// <returns>返回位图对象</returns>
            public Bitmap GetBitmap()
            {
                WebPageBitmap shot = new WebPageBitmap(this.Url, this.ScreenWidth, this.ScreenHeight);
                shot.GetIt();
                return shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
            }
        }
    }
}
